import request from '/@/utils/request';
import { BaseGeoServerType, BaseGeoServerMap, BaseGeoServerLight } from '/@/config/geoserver';
import { getUserAuthKey } from '/@/utils';
import X2JS from 'x2js';
import { Session } from '/@/utils/storage';
import { useUserInfo } from '/@/stores/userInfo';

/**
 * 获取边界数据
 * @param {*} para
 * @returns
 */
export function getGeoJsonFromGeoserver(para: any, layerName: string | undefined) {
	let key = `${Session.get('companyAppKey')},${layerName}`;
	let authkey = key ? `?authkey=${key}` : '';
	return request({
		url: `/geoserver/${BaseGeoServerType.value}/ows${authkey}`,
		method: 'GET',
		params: para,
	});
}

/**
 * 加载永农数据
 * @returns
 */
export function ynLandImage() {
	let authkey = getUserAuthKey(`${BaseGeoServerType.value}:farmland-2020`);
	let query = {
		format: 'image/png',
		VERSION: '1.1.1',
		layers: `${BaseGeoServerType.value}:farmland-2020`,
		exceptions: 'application/vnd.ogc.se_inimage',
		transparent: true,
		maxZoom: 21,
		pane: 'maxLevelPane',
	};
	if (authkey) {
		query['AuthKey'] = authkey;
	}
	return L.tileLayer.wms(`/geoserver/${BaseGeoServerType.value}/wms`, query);
}
/**
 * 加载高标农数据
 * @returns
 */
export function gbnLandImage() {
	let authkey = getUserAuthKey(`${BaseGeoServerType.value}:hignfarmland-2020`);
	let query = {
		format: 'image/png',
		VERSION: '1.1.1',
		layers: `${BaseGeoServerType.value}:hignfarmland-2020`,
		exceptions: 'application/vnd.ogc.se_inimage',
		transparent: true,
		maxZoom: 21,
		pane: 'maxLevelPane',
	};
	if (authkey) {
		query['AuthKey'] = authkey;
	}
	return L.tileLayer.wms(`/geoserver/${BaseGeoServerType.value}/wms`, query);
}

/**
 * 加载多光谱影像
 * @param {Array} wms  无人机影像
 * @returns {TileLayer.WMS}
 */
export function lightImage(wms) {
	let authkey = getUserAuthKey(wms[0]);
	let query = {
		format: 'image/png',
		VERSION: '1.1.1',
		layers: wms.join(','),
		exceptions: 'application/vnd.ogc.se_inimage',
		transparent: true,
		zIndex: 1,
	};
	if (authkey) {
		query['authkey'] = authkey;
	}
	return L.tileLayer.wms(`/geoserver/${BaseGeoServerLight.value}/wms`, query);
}

/**
 * 获取基地区域
 * @param {*} para
 * @returns
 */
export function getBaseArea() {
	let time = new Date().getTime();
	let authkey = getUserAuthKey(`${BaseGeoServerType.value}:dm_tree_base`);
	let query = {
		format: 'image/png',
		VERSION: '1.1.1',
		layers: `${BaseGeoServerType.value}:dm_tree_base`,
		exceptions: 'application/vnd.ogc.se_inimage',
		transparent: true,
		maxZoom: 21,
		pane: 'tilePane',
		time: time,
	};
	if (authkey) {
		query['authkey'] = authkey;
	}
	return L.tileLayer.wms(`/geoserver/${BaseGeoServerType.value}/wms`, query);
}

/**
 * 无人机影像云 -- 加载无人机影像
 * @param {*} wms
 * @returns
 */
export function uavOpenImage(wms, serverMap) {
	if (wms.length == 0) return;
	let companyAppKey = Session.get('uav_user_compant_appkey');
	return L.tileLayer.wms(`/opengeoserver/${serverMap}/wms`, {
		format: 'image/png',
		VERSION: '1.1.1',
		layers: wms.map((d) => `${serverMap}:${d}`),
		exceptions: 'application/vnd.ogc.se_inimage',
		transparent: true,
		zIndex: 1,
		authkey: `${companyAppKey},${serverMap}:${wms[0]}`,
	});
}

/**
 * 婺城苗木一张图
 * @returns
 */
export function getTreeLayerImage() {
	let time = new Date().getTime();
	// let authkey = `${Session.get('companyAppKey')},${BaseGeoServerType.value}:MS-330702-32-2025-979111`;
	let authkey = `${Session.get('companyAppKey')},${BaseGeoServerType.value}:MS-330702-34-2024-186303`;
	let query = {
		format: 'image/png',
		VERSION: '1.1.1',
		// layers: `${BaseGeoServerType.value}:MS-330702-32-2025-979111`,
		layers: `${BaseGeoServerType.value}:MS-330702-34-2024-186303`,
		exceptions: 'application/vnd.ogc.se_inimage',
		transparent: true,
		maxZoom: 21,
		pane: 'tilePane',
		time: time,
		zIndex: 200,
	};
	if (authkey) {
		query['authkey'] = authkey;
	}
	return L.tileLayer.wms(`/geoserver/${BaseGeoServerType.value}/wms`, query);
}
/**
 * 婺城商家证书一张图
 * @returns
 */
export function getShopIdLayerImage() {
	let time = new Date().getTime();
	// let authkey = `${Session.get('companyAppKey')},${BaseGeoServerType.value}:MS-330702-27-2025-522152`
	let authkey = `${Session.get('companyAppKey')},${BaseGeoServerType.value}:MS-330702-34-2024-184609`;
	let query = {
		format: 'image/png',
		VERSION: '1.1.1',
		// layers: `${BaseGeoServerType.value}:MS-330702-27-2025-522152`,
		layers: `${BaseGeoServerType.value}:MS-330702-34-2024-184609`,
		exceptions: 'application/vnd.ogc.se_inimage',
		transparent: true,
		maxZoom: 21,
		pane: 'tilePane',
		time: time,
		zIndex: 200,
	};
	if (authkey) {
		query['authkey'] = authkey;
	}
	return L.tileLayer.wms(`/geoserver/${BaseGeoServerType.value}/wms`, query);
}
